package com.wxapi

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.jaeger.library.StatusBarUtil
import com.module.frame.base.BaseFrameViewModel
import com.module.frame.base.BaseVmVdbActivity
import com.module.frame.ext.postEvent
import com.module.third.R
import com.module.third.bean.IThirdType
import com.module.third.bus.PayBus
import com.module.third.databinding.ThirdActivityWechatBinding
import com.module.third.ext.payLiveSharedFlow
import com.module.third.wx.WxConfig
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.wxapi.model.WXEntryViewModel

open class MyWXPayEntryActivity : BaseVmVdbActivity<BaseFrameViewModel, ViewDataBinding>(),
    IWXAPIEventHandler {

    private val api: IWXAPI by lazy {
        WXAPIFactory.createWXAPI(this, WxConfig.WX_APP_ID, false)
    }


    override fun bingViewModel() {
    }

    override fun getLayoutId(): Int {
        return R.layout.third_activity_wechat
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.window.decorView.setBackgroundColor(Color.TRANSPARENT)
    }

    protected fun setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null) //状态栏透明
        StatusBarUtil.setLightMode(this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        api.handleIntent(intent, this)
    }

    override fun initView() {
        super.initView()
        api.handleIntent(intent, this)
        setStatusBar()
    }

    override fun onReq(baseReq: BaseReq) {
    }

    override fun onResp(baseResp: BaseResp) {
        if (baseResp.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
            postEvent(payLiveSharedFlow, PayBus(IThirdType.IPayType.WX, baseResp.errCode))
        }
        finish()
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, 0)
    }
}